@model TieredPricePartViewModel

<h3>@T["Price tiers"]</h3>

<section id="price-tiers">
    <input type="hidden" name="@Html.NameFor(m => m.TieredValuesSerialized)" v-model="tieredValuesSerialized" />

    <div class="mb-3" asp-validation-class-for="DefaultPrice">
        <label asp-for="DefaultPrice">@T["Default Price"] </label>
        <div class="input-group">
            <input asp-for="DefaultPrice" class="form-control text-muted" />
            <div class="input-group-append">
                <select asp-for="Currency" class="form-select" v-model="currency"
                        asp-items="@(new SelectList(Model.Currencies, "CurrencyIsoCode", "CurrencyIsoCode"))"></select>
            </div>
        </div>
        <span asp-validation-for="DefaultPrice"></span>
    </div>

    <div class="mb-3" v-for="tier in tiers">
        <div class="row g-3 align-items-center">
            <div class="col-auto">
                <input type="text" class="form-control tier-quantity-editor" v-model="tier.Quantity" placeholder="@T["Quantity"]"/>
            </div>
            <div class="col-auto">
                <div class="input-group">
                    <input type="text" class="form-control tier-unit-price-editor" v-model="tier.UnitPrice" placeholder="@T["Unit Price"]"/>
                    <div class="input-group-append">
                        <input type="button" readonly="readonly" :value="currency" class="form-control"/>
                    </div>
                </div>
            </div>
            <div class="col-auto">
                <div class="btn btn-danger remove-price-tier-button" @@click="remove(tier)">@T["Remove"]</div>
            </div>
        </div>
    </div>
    <div class="btn btn-primary mb-3 add-price-tier-button" @@click="add()">@T["Add Price Tier"]</div>
    <span asp-validation-for="TieredValuesSerialized"></span>
</section>

<script at="Foot" asp-name="vuejs"></script>
<script at="Foot" depends-on="vuejs">
    let app = new Vue({
        el: "#price-tiers",
        data: {
            tiers: @(!string.IsNullOrEmpty(Model.TieredValuesSerialized) ? Html.Raw(Model.TieredValuesSerialized) : "[]"),
            currency: "@Model.Currency",
        },
        computed: {
            tieredValuesSerialized() {
                return JSON.stringify(this.tiers.map(tier => ({
                    Quantity: Number(tier.Quantity),
                    UnitPrice: Number(tier.UnitPrice)
                })));
            }
        },
        methods: {
            sort() {
                this.tiers.sort((a, b) => a.Quantity - b.Quantity);
            },
            remove(tier) {
                this.tiers.splice(this.tiers.indexOf(tier), 1);
                this.sort();
            },
            add() {
                const maxQuantity = Math.max(0, ...this.tiers.map(t => t.Quantity ?? 0));
                this.tiers.push({ Quantity: maxQuantity + 1, UnitPrice: "" });
                this.sort();
            }
        },
    });
</script>

